package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码使我快乐 Administrator on 2019/12/2.
 */

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

//    新增套餐
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setCheckgroupAndSetmeal(setmealId,checkgroupIds);

        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
    }




    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);

        String queryString = queryPageBean.getQueryString();
        if (queryString != null){
            queryString = "%"+queryString+"%";
            queryPageBean.setQueryString(queryString);
        }
        List<Setmeal> setmeals = setmealDao.findPage(queryPageBean);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmeals);

        long total = pageInfo.getTotal();
        return new PageResult(total,setmeals );
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
//        修改套餐的基础信息
        setmealDao.edit(setmeal);
//        清除检查组跟套餐表的联系
        setmealDao.deleteCheckGroupIds(setmeal.getId());
//         重新建立检查组跟套餐表的联系
        System.out.println(checkgroupIds.length);
        setCheckgroupAndSetmeal(setmeal.getId(),checkgroupIds);

    }

    @Override
    public void deleteSetmeal(Integer id) {

        setmealDao.deleteCheckGroupIds(id);

        setmealDao.deleteSetmeal(id);

    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public Setmeal findSetmealById(Integer id) {
        return setmealDao.findSetmealById(id);
    }


    @Override
    public List<Integer> findBySetmealIdsAndByCheckGroupIds(Integer id) {
        return setmealDao.findBySetmealIdsAndByCheckGroupIds(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }


    public void setCheckgroupAndSetmeal(Integer setmealId,Integer[] checkgroupIds){
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setCheckgroupAndSetmeal(map);
            }
        }
    }


    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

}
